package com.guldan.jaina.core;

import com.guldan.core.spring.ContextHolder;
import com.guldan.jaina.task.SyncRedisDataHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Silas.
 * Date: 2016/10/2
 * Time: 0:35
 */
public class Start {

    protected static Logger logger = LoggerFactory.getLogger(Start.class);

    public static boolean STARTED = false;

    /*程序入口*/
    public static void main(String[] args) {

        try {
            logger.warn("start run jaina provider.");
//            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/spring/app-context-*.xml");

            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[]{"spring/app-context-common.xml",
                            "spring/app-context-db.xml",
                            "spring/app-context-dubbo-provider.xml",
                            "spring/app-context-redis.xml",
                            "spring/app-context-task.xml"});

            ContextHolder.setCtx(context);
            context.start();
            logger.warn("jaina provider started successfully.");
            STARTED = true;

            //服务启动的时候，进行一次缓存刷新
            try {
                SyncRedisDataHandler syncRedisDataHandler = (SyncRedisDataHandler) context.getBean("syncRedisDataHandler");
                syncRedisDataHandler.handler();
            } catch (Exception ex) {
                logger.error("SyncRedisDataHandler.handler error " + ex.getMessage());
            }

            synchronized (Start.class) {
                while (true) {
                    Start.class.wait();
                }
            }
        } catch (Throwable e) {
            logger.error("start jaina provider failed.", e);
            return;
        }
    }
}
